package com.tdkj.service.risevalue.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.tdkj.dao.risevalue.IBeanPlateSMSSubscriptionDao;
import com.tdkj.model.financial.BeanConsumeRecord;
import com.tdkj.model.risevalue.BeanPlateQuotationSubscription;
import com.tdkj.model.risevalue.BeanPlateQuotationUseInfo;
import com.tdkj.model.risevalue.BeanPlateSMSCustom;
import com.tdkj.model.risevalue.BeanPlateSMSSubscription;
import com.tdkj.model.risevalue.BeanPlateSMSUseInfo;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.IBaseService.VirtualCurrencyType;
import com.tdkj.service.risevalue.IBeanPlateSMSSubscriptionService;

@Component
public class BeanPlateSMSSubscriptionServiceImpl extends BaseService implements
		IBeanPlateSMSSubscriptionService {
	
	private  IBeanPlateSMSSubscriptionDao beanPlateSMSSubscriptionDao;
    
	@Resource
	public void setBeanPlateSMSSubscriptionDao(IBeanPlateSMSSubscriptionDao beanPlateSMSSubscriptionDao) {
		this.beanPlateSMSSubscriptionDao = beanPlateSMSSubscriptionDao;
	}

	@SuppressWarnings("unchecked")
	public List<BeanPlateSMSSubscription> getByUserId(Long userId){
		
		BeanUser user = beanPlateSMSSubscriptionDao.findById(userId, BeanUser.class);
		return beanPlateSMSSubscriptionDao.getSession().createCriteria(BeanPlateSMSSubscription.class)
			.add(Restrictions.eq("user", user))
			.list();
	}
	
	public void createSubscription(Long userId, Long customId){
		BeanUser user = beanPlateSMSSubscriptionDao.findById(userId, BeanUser.class);
		BeanPlateSMSCustom custom = beanPlateSMSSubscriptionDao.findById(customId, BeanPlateSMSCustom.class);
		
		boolean isSuccess = false;
		VirtualCurrencyType type = VirtualCurrencyType.valueOf(custom.getUnit());
		int price = custom.getPrice().intValue();
		if(VirtualCurrencyType.金币.equals(type)){
			if(user.getFinancialAccount().getGold() >= price){
				user.getFinancialAccount().setGold(user.getFinancialAccount().getGold() - price);
				isSuccess = true;
			}
			
		}else if(VirtualCurrencyType.银币.equals(type)){
			if(user.getFinancialAccount().getSilver() >= price){
				user.getFinancialAccount().setSilver(user.getFinancialAccount().getSilver() - price);
				isSuccess = true;
			}
		}else if(VirtualCurrencyType.矿币.equals(type)){
			if(user.getFinancialAccount().getMine() >= price){
				user.getFinancialAccount().setMine(user.getFinancialAccount().getMine() - price);
				isSuccess = true;
			}
		}
		
		
		if(isSuccess){
			
			BeanPlateSMSSubscription bean = new BeanPlateSMSSubscription();
			bean.setBuyTime(new Timestamp(System.currentTimeMillis()));
			bean.setUser(user);
			bean.setCustom(custom);
			
			beanPlateSMSSubscriptionDao.save(bean);
			
			Long seconds = 0L;
			
			if("年".equals(custom.getTimeLimitUnit())){
				seconds = 365*24*60*60*custom.getTimeLimitAmount().longValueExact();
			}else if("月".equals(custom.getTimeLimitUnit())){
				seconds = 30*24*60*60*custom.getTimeLimitAmount().longValueExact();
			}else if("日".equals(custom.getTimeLimitUnit())){
				seconds = 24*60*60*custom.getTimeLimitAmount().longValueExact();
			}
			
			BeanPlateSMSUseInfo info = null;
			if(user.getQuotationUseInfos() != null){
				for(BeanPlateSMSUseInfo infoTemp : user.getSMSUseInfos()){
					if(infoTemp.getPlate().getId() == custom.getPlate().getId()){
						info = infoTemp;
					}
				}
			}
			
			if(info == null){
				info = new BeanPlateSMSUseInfo();
				info.setUser(user);
				info.setPlate(custom.getPlate());
				info.setLimitTime(0L);
				beanPlateSMSSubscriptionDao.save(info);
			}
			info.setLimitTime(info.getLimitTime() + seconds);
			
			BeanConsumeRecord record = new BeanConsumeRecord();
			record.setUser(user);
			record.setDescription(bean.getId() + ":" + bean.getClass().getName());
			record.setDateTime(new Timestamp(System.currentTimeMillis()));
			record.setMoney(-price);
			record.setUnit(type.name());
			beanPlateSMSSubscriptionDao.save(record);
		}
	}
}
